package com.artPreter.service.impl;

import com.artPreter.pojo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.mapper.ContentArtworkRelativeMapper;
import com.artPreter.mapper.ArtworkMapper;
import com.artPreter.service.IArtworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 艺术品表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-14
 */
@Service
public class ArtworkServiceImpl extends ServiceImpl<ArtworkMapper, Artwork> implements IArtworkService {


    @Override
    public List<Artwork> getIndexArtworks(int i) {
        List<Artwork> result =new ArrayList<>();
        if(i>0){
            LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
            artworkLambdaQueryWrapper.eq(Artwork::getIsIndex,1);
            artworkLambdaQueryWrapper.eq(Artwork::getStatus,1);
            artworkLambdaQueryWrapper.orderByDesc(Artwork::getIndexSort);
            artworkLambdaQueryWrapper.last("limit "+i );
            result = this.baseMapper.selectList(artworkLambdaQueryWrapper);
        }
        return result;
    }

    @Override
    public List<Artwork> getBannerArtworks(int i) {
        List<Artwork> result =new ArrayList<>();
        if(i>0) {
            LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
            artworkLambdaQueryWrapper.eq(Artwork::getIsBanner, 1);
            artworkLambdaQueryWrapper.eq(Artwork::getStatus,1);
            artworkLambdaQueryWrapper.orderByDesc(Artwork::getRecommendSort);
            artworkLambdaQueryWrapper.last("limit " + i);
            result = this.baseMapper.selectList(artworkLambdaQueryWrapper);
        }
        return result;
    }

    @Override
    public List<Artwork> getRelatedArtworkList(DepartContentSection departContentSection, Integer count) {
        List<Artwork> result = new ArrayList<>();
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        if(count >1){
            artworkQueryWrapper.last("limit "+count);
        }
        artworkQueryWrapper.eq("s.content_id",departContentSection.getContentId());
        result = this.baseMapper.getRelatedArtworkList(artworkQueryWrapper);

        return result;
    }

    @Override
    public List<Artwork> getArtistRelatedArtworkList(ArtistContentSection artistContentSection, Integer count) {
        List<Artwork> result = new ArrayList<>();
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        if(count >1){
            artworkQueryWrapper.last("limit "+count);
        }
        artworkQueryWrapper.eq("s.content_id",artistContentSection.getContentId());
        result = this.baseMapper.getArtistRelatedArtworkList(artworkQueryWrapper);

        return result;
    }

    @Override
    public List<Artwork> getArtworkByName(Artwork artwork) {
        LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            artworkLambdaQueryWrapper.like(Artwork::getName,artwork.getName());
        }
        if(artwork.getArtwoktType()!=null&&!"".equals(artwork.getArtwoktType())){
            artworkLambdaQueryWrapper.eq(Artwork::getArtwoktType,artwork.getArtwoktType());
        }
        List<Artwork> result = this.baseMapper.selectList(artworkLambdaQueryWrapper);
        return result;
    }

    @Override
    public IPage<Artwork> getPage(Integer currentPage, Integer artWorkSize, Artwork artwork) {
        IPage<Artwork> artworkIPage = new Page<>(currentPage,artWorkSize);
        LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            artworkLambdaQueryWrapper.like(Artwork::getName,artwork.getName());
        }
        if(artwork.getArtwoktType()!=null&&!"".equals(artwork.getArtwoktType())){
            artworkLambdaQueryWrapper.eq(Artwork::getArtwoktType,artwork.getArtwoktType());
        }
        if(artwork.getType()!=null){
            if(artwork.getType().equals(-1)){
                artworkLambdaQueryWrapper.eq(Artwork::getType,1);
                artworkLambdaQueryWrapper.isNotNull(Artwork::getDepartNames);
                artworkLambdaQueryWrapper.ne(Artwork::getDepartNames,"");

            }else{
                artworkLambdaQueryWrapper.eq(Artwork::getType,artwork.getType());
            }
        }
        artworkIPage = this.baseMapper.selectPage(artworkIPage,artworkLambdaQueryWrapper);
        return artworkIPage;
    }

    @Override
    public Artwork getArtWorkById(Integer id) {
        Artwork artwork = this.baseMapper.selectById(id);
        return artwork;
    }

    /**
     * 11111
     * @param searchItem
     * @param currentPage
     * @return
     */
    @Override
    public IPage<Artwork> getRelatedArtworkPageList(Artwork searchItem, Integer currentPage,Integer size) {
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        IPage<Artwork> artworkIPage = new Page<>(currentPage,size);
        artworkQueryWrapper.eq("s.content_id",searchItem.getContentId());
        if(searchItem.getArtwoktType()!=null&& !searchItem.getArtwoktType().isEmpty()){
            artworkQueryWrapper.eq("b.artwokt_type",searchItem.getArtwoktType());
        }
        if(searchItem.getName()!=null&&!searchItem.getName().equals("")){
            artworkQueryWrapper.like("b.name",searchItem.getName());

        }
        artworkIPage = this.baseMapper.getRelatedArtworkPageList(artworkIPage,artworkQueryWrapper);
        return artworkIPage;
    }

    @Override
    public List<ArtType> getArtistRelatedArtworkArtTypegetPageList(Integer currentPage, Integer artWorkSize, Artwork artwork) {
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        artworkQueryWrapper.eq("s.content_id",artwork.getContentId());
        List<ArtType> result = this.baseMapper.getArtistRelatedArtworkArtTypegetPageList(artworkQueryWrapper);
        return result;
    }

    @Override
    public IPage<Artwork> getEventExhibitionRelatedArtworkPageList(Artwork searchItem, Integer currentPage) {
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        IPage<Artwork> artworkIPage = new Page<>(currentPage,16);
        // 艺术机构
        artworkQueryWrapper.eq("s.content_id",searchItem.getContentId());
        // 作品名字
        if(searchItem.getName()!=null&&!searchItem.getName().equals("")){
            artworkQueryWrapper.like("b.name",searchItem.getName());
        }
        // 艺术种类
        if(searchItem.getArtwoktType()!=null&&!searchItem.getArtwoktType().isEmpty()){
            artworkQueryWrapper.eq("b.artwokt_type",searchItem.getArtwoktType());
        }
        // 排序
        artworkQueryWrapper.orderByDesc("b.sort");
        artworkIPage = this.baseMapper.getEventExhibitionRelatedArtworkPageList(artworkIPage,artworkQueryWrapper);
        return artworkIPage;
    }

    @Override
    public List<Artwork> getEventExhibitionRelatedArtworkList(EventExhibitionContentSection eventExhibitionContentSection, Integer count) {
        List<Artwork> result = new ArrayList<>();
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        if(count >1){
            artworkQueryWrapper.last("limit "+count);
        }
        artworkQueryWrapper.eq("s.content_id",eventExhibitionContentSection.getContentId());
        result = this.baseMapper.getRelatedArtworkList(artworkQueryWrapper);

        return result;
    }

    @Override
    public List<Artwork> getRelatedArtworkArtTypegetList(Artwork searchItem) {
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        artworkQueryWrapper.eq("s.content_id",searchItem.getContentId());
        List<Artwork> result = this.baseMapper.getRelatedArtworkArtTypegetList(artworkQueryWrapper);
        return result;
    }

    @Override
    public IPage<Artwork> getDepartRelatedArtworkPageList(Artwork searchItem, Integer currentPage,Integer size) {
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        IPage<Artwork> artworkIPage = new Page<>(currentPage,size);
        if(searchItem.getDepartId()==null&&!searchItem.getDepartId().equals("")){
            return artworkIPage;
        }else{
            artworkQueryWrapper.like("depart_ids",searchItem.getDepartId());
        }
        if(searchItem.getType()!=null){
            artworkQueryWrapper.eq("type",searchItem.getType());
        }
        // 艺术类型
        if(searchItem.getArtwoktType()!=null){
            artworkQueryWrapper.eq("artwokt_type",searchItem.getArtwoktType());
        }
        // 名称模糊搜索
        if(searchItem.getName()!=null&&!searchItem.getName().equals("")){
            artworkQueryWrapper.like("name",searchItem.getName());
        }
        // 排序
        artworkQueryWrapper.orderByDesc("sort");
        artworkIPage = this.baseMapper.selectPage(artworkIPage,artworkQueryWrapper);

        return artworkIPage;
    }

    @Override
    public List<Artwork> getDepartRelatedArtworkTypegetList(Artwork searchItem) {
        List<Artwork> result = new ArrayList<>();
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        if(searchItem.getDepartId()==null){
            return result;
        }else{
            artworkQueryWrapper.eq("depart_ids",searchItem.getDepartId());
        }
        if(searchItem.getType()!=null){
            artworkQueryWrapper.eq("type",searchItem.getType());
        }
        result = this.baseMapper.getDepartRelatedArtworkTypegetList(artworkQueryWrapper);
        return result;
    }

    @Override
    public List<Artwork> getRecommendArtworks(int i) {
        List<Artwork> result =new ArrayList<>();
        if(i>0) {
            LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
            artworkLambdaQueryWrapper.eq(Artwork::getIsRecommend, 1);
            artworkLambdaQueryWrapper.eq(Artwork::getStatus,1);
            artworkLambdaQueryWrapper.orderByDesc(Artwork::getRecommendSort);
            artworkLambdaQueryWrapper.last("limit " + i);
            result = this.baseMapper.selectList(artworkLambdaQueryWrapper);
        }
        return result;
    }
}
